package com.insigma.business.qrcode.soldier.util;

import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;

import java.io.ByteArrayInputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class QueryResult {

	
	public static<T> List<T> readXMLToList(String tr2, String s, Class<T> clazz) {

	    List<T> list = new ArrayList();
	    try {
	        
	        SAXReader reader = new SAXReader();
			
	        org.dom4j.Document doc = reader.read(new InputSource(new ByteArrayInputStream(s.getBytes("UTF-8"))));
	        
	        Element root = doc.getRootElement();
	        
			Element content = root.element("CONTENT");
	        Element body = content.element("LIST");
			

	        
	        Element foo;
	        for (Iterator i = body.elementIterator(tr2); i.hasNext();) {
	            foo = (Element) i.next();
	            
	            T obj = clazz.newInstance();
	            Field[] properties = obj.getClass().getDeclaredFields();
	            
	            Method getmeth;
	            
	            Method setmeth;
	            
	            for (int j = 0; j < properties.length; j++) {
	                
	                if (properties[j].getName().equals("serialVersionUID")) {
	                    continue;
	                } else {
	                    setmeth = obj.getClass().getMethod(
	                            "set"
	                                    + properties[j].getName().substring(0, 1).toUpperCase()
	                                    + properties[j].getName().substring(1), properties[j].getType());
	                    Object setStr = foo.elementText(properties[j].getName());

	                    if (foo.elementText(properties[j].getName()) != null&&!"".equals(foo.elementText(properties[j].getName()))) {
	                        if (properties[j].getType() == Date.class) {
	                            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	                            Date sj = df.parse(foo.elementText(properties[j].getName()));
	                            setStr = sj;
	                        } else if (properties[j].getType() == Integer.class) {
	                            setStr = Integer.parseInt(foo.elementText(properties[j].getName()));
	                        } else if (properties[j].getType() == Character.class) {
	                            setStr = foo.elementText(properties[j].getName()).charAt(0);
	                        }else if (properties[j].getType() == Double.class) {
	                            setStr = Double.parseDouble(foo.elementText(properties[j].getName()));
	                        }
	                    } else {
	                        setStr = null;
	                    }
	                    
	                    setmeth.invoke(obj, setStr);
	                }
	            }
	            list.add(obj);
	        }
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return list;
	}
}
